package tgc.edu.zz.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.zz.service.LogService;
import tgc.edu.zz.service.PersionService;
import tgc.edu.zz.vo.LoginLogVo;
import tgc.edu.zz.vo.PersionVo;

@Controller
@RequestMapping("/") 
public class MainController {
	
	@Autowired
	private PersionService persionService;
	@Autowired
	private LogService logService;

	/**
	 * @return 通过后台返回到欢迎页面首页（用户看到的）
	 */
	@RequestMapping("/")
	public String welcome() {
		return "main/welcome";
	}
	
	/**
	 * @param username
	 * @param password
	 * @param session
	 * @param request
	 * @param mod
	 * @return 输入用户名和密码进入后台管理页面（管理员可见）
	 */
	@RequestMapping("/index")
	public String login(String username,String password,HttpSession session,HttpServletRequest request,Model mod) {
		PersionVo model = persionService.findByUsernameAndPassword(username,password);
		if(model != null) {
			session.setAttribute("model", model);
			LoginLogVo log = new LoginLogVo();
			log.setUsername(model.getUsername());
			log.setIdentity(model.getIdentity());
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			log.setLoginTime(df.format(new Date()));
			logService.save(log);
			return "main/index";
		}else {
			mod.addAttribute("error", "账号或密码错误！");
			return "redirect:/";
		}
		
	}
	
	/**
	 * 
	 * @return 个人介绍页面
	 */
	@RequestMapping("/about")
	public String about() {
		return "main/about";
		
	}
}
